<template>
	<view class="legal-risk-page">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar">
			<view class="navbar-content">
				<image class="back-icon" src="https://apizx01.sc798.com/Resource/home/left.png" mode="aspectFit" @click="goBack"></image>
				<text class="navbar-title">{{ pageTitle }}</text>
				<view class="navbar-placeholder"></view>
			</view>
		</view>
		
		<!-- 页面内容 -->
		<view class="page-content">
			<!-- 加载状态 -->
			<view v-if="isLoading" class="loading-container">
				<text class="loading-text">加载中...</text>
			</view>
			
			<!-- 有数据时显示 -->
			<template v-else>
				<!-- 统计信息 -->
				<view class="stats-info">
					<text class="stats-text">共<text class="stats-number">{{ totalCount }}</text>条{{ pageTitle }}</text>
				</view>
				
				<!-- 执行信息列表 -->
					<scroll-view 
						class="info-list" 
						v-if="executionList && executionList.length > 0"
						scroll-y
						@scrolltolower="loadMoreData"
						:lower-threshold="100"
					>
						<!-- 加载更多状态 -->
					<view v-if="(category === '01' || category === '02' || category === '03' || category === '04' || category === '06' || category === '07' || category === '08' || category === '09' || category === '10') && isLoadingMore" class="loading-more">
						<text>正在加载更多...</text>
					</view>
					<!-- 执行信息显示(category=02) -->
				<template v-if="category === '02'">
					<view 
						class="info-item" 
						v-for="(item, index) in executionList" 
						:key="index"
					>
						<view class="info-row">
							<text class="info-label">案号</text>
							<text class="info-content">{{ item.caseNumber || '-' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">执行法院</text>
							<text class="info-content">{{ item.court || '-' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">执行标的</text>
							<text class="info-content">{{ item.amount || '-' }}{{ item.amount ? '元' : '' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">执行依据文号</text>
							<text class="info-content">{{ item.caseNumber || '-' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">立案日期</text>
							<text class="info-content">{{ item.caseDate || '-' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">执行状态</text>
							<text class="info-content">{{ item.status || '-' }}</text>
						</view>
					</view>
				</template>

				<!-- 失信信息显示(category=01) -->
				<template v-if="category === '01'">
						<view 
							class="info-item" 
							v-for="(item, index) in executionList" 
							:key="index"
						>
							<view class="info-row">
								<text class="info-label">案号</text>
								<text class="info-content">{{ item.caseNumber || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">执行法院</text>
								<text class="info-content">{{ item.court || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">履行情况</text>
								<text class="info-content">{{ item.executionStatus || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">执行依据文号</text>
								<text class="info-content">{{ item.caseNumber || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">立案日期</text>
								<text class="info-content">{{ item.date || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">发布日期</text>
								<text class="info-content">{{ item.publishDate || '-' }}</text>
							</view>
						</view>
					</template>
					
					<!-- 开庭公告显示(category=06) -->
					<template v-else-if="category === '06'">
						<view 
							class="info-item" 
							v-for="(item, index) in executionList" 
							:key="index"
						>
							<view class="info-row">
								<text class="info-label">案号</text>
								<text class="info-content">{{ item.caseNo || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">案由</text>
								<text class="info-content">{{ item.caseReason || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">当事人</text>
								<text class="info-content" style="white-space: pre-wrap;">{{ item.yuangaoStr ? '原告：' + item.yuangaoStr : '' }}{{ item.yuangaoStr && item.beigaoStr ? '\n' : '' }}{{ item.beigaoStr ? '被告：' + item.beigaoStr : '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">开庭日期</text>
								<text class="info-content">{{ item.hearingDate || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">法院</text>
								<text class="info-content">{{ item.court || '-' }}</text>
							</view>
						</view>
					</template>
					
					<!-- 法院公告显示(category=07) -->
					<template v-else-if="category === '07'">
						<view 
							class="info-item" 
							v-for="(item, index) in executionList" 
							:key="index"
						>
							<view class="info-row">
								<text class="info-label">案号</text>
								<text class="info-content">{{ item.caseNo || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">当事人</text>
								<text class="info-content" style="white-space: pre-wrap;">{{ item.people || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">公告日期</text>
								<text class="info-content">{{ item.date || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">公告类型</text>
								<text class="info-content">{{ item.type || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">公告法院</text>
								<text class="info-content">{{ item.court || '-' }}</text>
							</view>
						</view>
					</template>
				
				<!-- 股权冻结显示(category=08) -->
									<template v-else-if="category === '08'">
										<view 
											class="info-item" 
											v-for="(item, index) in executionList" 
											:key="index"
										>
											<view class="info-row">
												<text class="info-label">公示日期</text>
												<text class="info-content">{{ item.detailFreezeStartDate || '-' }}</text>
											</view>
											<view class="info-row">
												<text class="info-label">执行通知书</text>
												<text class="info-content">{{ item.detailAdjudicateNo || item.number || '-' }}</text>
											</view>
											<view class="info-row">
												<text class="info-label">被执行人名称</text>
												<text class="info-content">{{ item.detailAssistName || '-' }}</text>
											</view>
											<view class="info-row">
												<text class="info-label">股权数额</text>
												<text class="info-content">{{ item.detailFreezeAmount || item.amount || '-' }}</text>
											</view>
											<view class="info-row">
												<text class="info-label">冻结状态</text>
												<text class="info-content">{{ item.status || '-' }}</text>
											</view>
											<view class="info-row">
												<text class="info-label">结束时间</text>
												<text class="info-content">{{ item.detailFreezeEndDate || '-' }}</text>
											</view>
											<view class="info-row">
												<text class="info-label">执行法院</text>
												<text class="info-content">{{ item.detailExecuteCourt || item.executiveCourt || '-' }}</text>
											</view>
						
					</view>
				</template>

				<!-- 裁判文书显示(category=03) -->
				<template v-else-if="category === '03'">
						<view 
							class="info-item" 
							v-for="(item, index) in executionList" 
							:key="index"
						>
							<view class="info-row">
								<text class="info-label">案件名称</text>
								<text class="info-content">{{ item.title || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">相关案号</text>
								<text class="info-content">{{ item.caseNo || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">案件类型</text>
								<text class="info-content">{{ item.caseCause || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">案由</text>
								<text class="info-content">{{ item.caseType || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">案号</text>
								<text class="info-content">{{ item.caseNo || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">涉案身份</text>
								<text class="info-content">{{ item.role || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">判决日期</text>
								<text class="info-content">{{ item.date || '-' }}</text>
							</view>
							<view class="info-row">
								<text class="info-label">发布日期</text>
								<text class="info-content">{{ item.pubDate || '-' }}</text>
							</view>
						</view>
					</template>
				
				<!-- 限制消费令显示(category=04) -->
						<template v-else-if="category === '04'">
							<view 
								class="info-item" 
								v-for="(item, index) in executionList" 
								:key="index"
							>
								<view class="info-row">
									<text class="info-label">案号</text>
									<text class="info-content">{{ item.caseNo || '-' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">限制令对象</text>
									<text class="info-content">{{ item.companyName || '-' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">限消人员</text>
									<text class="info-content">{{ item.name || '-' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">立案日期</text>
									<text class="info-content">{{ item.filingDate || '-' }}</text>
								</view>
							</view>
						</template>
				<!-- 终本案件显示(category=09) -->
				<template v-else-if="category === '09'">
							<view 
								class="info-item" 
								v-for="(item, index) in executionList" 
								:key="index"
							>
								<view class="info-row">
									<text class="info-label">终本时间</text>
									<text class="info-content">{{ item.terminateDate || '-' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">立案时间</text>
									<text class="info-content">{{ item.filingDate || '-' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">终本案号</text>
									<text class="info-content">{{ item.caseNoTerminal || '-' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">执行标的</text>
									<text class="info-content">{{ item.amount || '-' }}{{ item.amount ? '元' : '' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">未履行标的</text>
									<text class="info-content">{{ item.failPerformAmount || '-' }}{{ item.failPerformAmount ? '元' : '' }}</text>
								</view>
								<view class="info-row">
									<text class="info-label">执行法院</text>
									<text class="info-content">{{ item.court || '-' }}</text>
								</view>
							</view>
						</template>

				<!-- 立案信息显示(category=10) -->
				<template v-else-if="category === '10'">
					<view 
						class="info-item" 
						v-for="(item, index) in executionList" 
						:key="index"
					>
						<view class="info-row">
							<text class="info-label">案号</text>
							<text class="info-content">{{ item.caseNumber || '-' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">当事人</text>
							<text class="info-content">{{ item.name || '-' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">立案日期</text>
							<text class="info-content">{{ item.date || item.filingDate || '-' }}</text>
						</view>
						<view class="info-row">
							<text class="info-label">法院</text>
							<text class="info-content">{{ item.court || '-' }}</text>
						</view>
					</view>
				</template>

						<!-- 没有更多数据提示 - 放在所有数据模板之后 -->
					<view v-if="(category === '01' || category === '02' || category === '03' || category === '04' || category === '06' || category === '07' || category === '08' || category === '09' || category === '10') && !hasMore && executionList && executionList.length > 0" class="no-more">
						<text>没有更多数据了</text>
					</view>
				</scroll-view>
				
				<!-- 空状态 -->
				<view v-else class="empty-container">
					<text class="empty-text">暂无相关数据</text>
				</view>
			</template>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted, computed, watch } from 'vue';

// 页面标题
const pageTitle = ref('司法风险')
// 企业信用代码
const creditcode = ref('')
// 类别参数（01:失信信息, 02:执行信息, 03:裁判文书, 04:限制消费令, 06:开庭公告, 07:法院公告, 08:股权冻结, 09:终本案件, 10:立案信息）
const category = ref('01')
// 总条数
const totalCount = ref(0)
// 执行信息列表
const executionList = ref([])
// 加载状态
const isLoading = ref(false)
// 加载更多状态
const isLoadingMore = ref(false)
// 是否有更多数据
const hasMore = ref(true)
// 当前页码
const pageNum = ref(1)
// 每页大小
const pageSize = ref(10)

// 返回上一页
const goBack = () => {
	uni.navigateBack()
}

// 专用的股权冻结数据获取函数
	const fetchEquityFreezeData = async () => {
		console.log('获取股权冻结数据...');
		isLoading.value = true;
		try {
			const equityRequestData = {
				creditcode: creditcode.value,
				category: '08', // 固定为股权冻结
				ishistorycode: '',
				pageNum: pageNum.value,
				pageSize: pageSize.value
			};
			console.log('股权冻结专用请求参数:', equityRequestData);
			
			const response = await uni.request({
				url: 'https://apizx01.sc798.com/api/proxy/enterprise/basic/managerisklist',
				method: 'POST',
				data: equityRequestData,
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				}
			});
			
			console.log('股权冻结响应:', response);

			// 确保executionList始终是数组
			if (!Array.isArray(executionList.value)) {
				executionList.value = [];
			}
			
			if (response.statusCode === 200) {
				const resData = response.data;
				// 直接使用rows字段作为数据列表
				if (resData.rows && Array.isArray(resData.rows)) {
					// 更新总条数
					totalCount.value = resData.total || 0;
					
					// 处理返回的股权冻结数据
					const equityData = resData.rows;
					if (pageNum.value === 1) {
						executionList.value = equityData;
					} else {
						executionList.value = [...executionList.value, ...equityData];
					}
					
					// 增加页码
					pageNum.value++;
					console.log('股权冻结数据加载成功，共', equityData.length, '条');
				} else {
					// 如果rows不存在但有数据，尝试使用其他可能的结构
					console.log('股权冻结响应数据结构:', resData);
					totalCount.value = 0;
					executionList.value = [];
				}
			} else {
				console.error('股权冻结请求失败:', response.statusCode);
				uni.showToast({
					title: '网络请求失败',
					icon: 'none'
				});
			}
		} catch (error) {
			console.error('股权冻结请求异常:', error);
			uni.showToast({
				title: '请求异常，请重试',
				icon: 'none'
			});
		} finally {
			isLoading.value = false;
			isLoadingMore.value = false; // 确保加载更多状态也被重置
		}
	};

// 加载更多数据
const loadMoreData = async () => {
	console.log('触发加载更多');
	// 失信信息、执行信息、裁判文书、限制消费令、开庭公告、法院公告、立案信息或终本案件页面且有更多数据且当前不在加载状态时才加载更多
	if ((category.value === '01' || category.value === '02' || category.value === '03' || category.value === '04' || category.value === '06' || category.value === '07' || category.value === '08' || category.value === '09' || category.value === '10') && hasMore.value && !isLoadingMore.value && !isLoading.value) {
		isLoadingMore.value = true;
		// 根据category调用不同的函数
		if (category.value === '08') {
			await fetchEquityFreezeData();
		} else {
			await fetchLegalRiskData();
		}
	}
}

// 监听category变化，重置分页状态
watch(category, (newVal) => {
	// 切换类别时重置分页状态
	pageNum.value = 1;
	executionList.value = [];
	totalCount.value = 0;
	hasMore.value = true;
	isLoadingMore.value = false;
	console.log('类别切换为:', newVal, '重置分页状态');
});

// 判断是否支持加载更多
const isLoadMoreSupported = computed(() => {
	return ['01', '02', '03', '04', '06', '07', '08', '09', '10'].includes(category.value);
});



// 获取法律风险数据
const fetchLegalRiskData = async () => {
	if (!creditcode.value) {
		console.error('信用代码不存在')
		return
	}
	
	// 加载更多时不显示主加载状态
	if (!isLoadingMore.value) {
		isLoading.value = true
	}
	try {
		// 确保请求参数包含所有必要字段
			const requestData = {
				creditcode: creditcode.value,
				category: category.value,
				ishistorycode: '',
				pageNum: pageNum.value,
				pageSize: pageSize.value
			};
			console.log('法律风险请求参数:', requestData);
	
		console.log('发送法律风险请求参数:', requestData);
	
		// 发送请求
			const response = await uni.request({
				url: 'https://apizx01.sc798.com/api/proxy/enterprise/basic/legalrisklist',
				method: 'POST',
				data: requestData,
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				}
			});
	
		console.log('法律风险响应:', response);

		// 确保executionList始终是数组
		if (!Array.isArray(executionList.value)) {
			executionList.value = [];
		}
	
		// 更新数据，根据实际返回的数据结构
		if (response.data && response.data.rows) {
			// 如果是加载更多，则追加数据
			if (isLoadingMore.value) {
				executionList.value = [...executionList.value, ...response.data.rows];
				// 加载更多成功后递增页码
				pageNum.value++;
			} else {
				// 新搜索：替换数据
				executionList.value = response.data.rows;
			}
			totalCount.value = response.data.total || response.data.rows.length;
			// 判断是否还有更多数据
			hasMore.value = executionList.value.length < totalCount.value;
			console.log('当前页码:', pageNum.value, '数据总数:', totalCount.value, '列表长度:', executionList.value.length, '是否有更多:', hasMore.value);
		} else {
			if (!isLoadingMore.value) {
				executionList.value = [];
				totalCount.value = 0;
			}
			hasMore.value = false;
			console.log('无数据返回，设置hasMore为false');
		}
	} catch (error) {
		console.error('获取法律风险数据失败:', error)
		uni.showToast({
			title: '获取数据失败，请重试',
			icon: 'none'
		})
	} finally {
		isLoading.value = false;
		isLoadingMore.value = false;
	}
}

// 页面挂载时获取传递的参数并请求数据
onMounted(async () => {
	// 获取传递的参数
	const pages = getCurrentPages()
	const currentPage = pages[pages.length - 1]
	if (currentPage.options) {
		// 设置页面标题
		if (currentPage.options.title) {
			pageTitle.value = decodeURIComponent(currentPage.options.title)
		}
		// 获取信用代码
		if (currentPage.options.creditcode) {
			creditcode.value = currentPage.options.creditcode
		}
		// 获取类别参数
		if (currentPage.options.category) {
			category.value = currentPage.options.category
		}
	}
	
	// 重置分页参数
	pageNum.value = 1;
	executionList.value = [];
	hasMore.value = true;
	
	// 根据category选择不同的数据获取函数
	if (category.value === '08') {
		await fetchEquityFreezeData();
	} else {
		await fetchLegalRiskData();
	}
})
</script>

<style lang="scss" scoped>
.legal-risk-page {
	min-height: 100vh;
	background-color: #f5f5f5;
}

.custom-navbar {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 1000;
	background-color: #ffffff;
	
	.navbar-content {
		display: flex;
		align-items: center;
        // justify-content: center;
		height: 210rpx;
		padding: 0 24rpx;
		padding-top: 44rpx;
        box-sizing: border-box;
		.back-icon {
			width: 48rpx;
			height: 48rpx;
		}
		
		.navbar-title {
			flex: 1;
			text-align: center;
			font-family: 'Alibaba PuHuiTi', sans-serif;
			font-weight: 500;
			font-size: 32rpx;
			color: #000000;
		}
		
		.navbar-placeholder {
			width: 32rpx;
			height: 32rpx;
		}
	}
}

.page-content {
	padding-top: 210rpx;
	padding-bottom: 40rpx;
}

.stats-info {
	margin: 33rpx 0 11rpx 53rpx;
	
	.stats-text {
		font-family: 'Alibaba PuHuiTi', sans-serif;
		font-weight: 400;
		font-size: 24rpx;
		color: #2B6FEA;
		
		.stats-number {
			color: #FF0000;
		}
	}
}

.info-list {
	
	display: flex;
	flex-direction: column;
	gap: 23rpx; /* 修改为23rpx的间距 */
	height: calc(100vh - 210rpx - 100rpx); /* 减去导航栏高度和统计信息高度 */
}

/* 加载更多样式 */
.loading-more {
	padding: 20rpx;
	text-align: center;
	color: #999;
	font-size: 28rpx;
	background-color: transparent;
}

.no-more {
	padding: 20rpx;
	text-align: center;
	color: #999;
	font-size: 28rpx;
	background-color: #f8f8f8;
	margin-top: 10rpx;
}

/* scroll-view样式 */
.info-list {
	height: calc(100vh - 300rpx);
}

/* 加载状态样式 */
.loading-container {
	display: flex;
	justify-content: center;
	align-items: center;
	min-height: 400rpx;
}

.loading-text {
	font-family: 'Alibaba PuHuiTi', sans-serif;
	font-size: 28rpx;
	color: #666666;
}

/* 空状态样式 */
.empty-container {
	display: flex;
	justify-content: center;
	align-items: center;
	min-height: 400rpx;
}

.empty-text {
	font-family: 'Alibaba PuHuiTi', sans-serif;
	font-size: 28rpx;
	color: #999999;
}

.info-item {
	width: 704rpx;
	background: #FFFFFF;
	border-radius: 20rpx;
	padding: 24rpx 46rpx 24rpx 46rpx; /* 左右padding均衡，确保居中 */
	box-sizing: border-box;
	margin: 23rpx auto;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	min-height: 280rpx;
}

.info-row {
		display: flex;
		align-items: flex-start;
		margin-bottom: 20rpx;
		
		.info-label {
					font-family: 'Alibaba PuHuiTi', sans-serif;
					font-weight: 400;
					font-size: 24rpx;
					color: #9B9B9B;
					line-height: 39rpx;
					width: 160rpx;
					flex-shrink: 0;
					text-align: left;
					padding-right: 20rpx;
					box-sizing: border-box;
				}
		
		.info-content {
			font-family: 'Alibaba PuHuiTi', sans-serif;
			font-weight: 400;
			font-size: 24rpx;
			color: #000000;
			line-height: 39rpx;
			flex: 1;
			word-break: break-all;
		}
	}
</style>
